<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="用户类型" prop="utype">
        <el-select style="width: 230px" v-model="queryParams.utype" placeholder="请选择用户类型" clearable>
          <el-option
            v-for="dict in dict.type.wx_usr_utype"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          style="width: 230px"
          v-model="queryParams.phone"
          placeholder="请输入手机"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户昵称" prop="nickname">
        <el-input
          style="width: 230px"
          v-model="queryParams.nickname"
          placeholder="请输入昵称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="用户状态" prop="status">
        <el-select style="width: 230px" v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.wx_user_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['wexin:user:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wexin:user:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['wexin:user:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="用户编号" align="center" prop="id" v-if="true" :show-overflow-tooltip="true" />
      <el-table-column label="unionid" align="center" prop="unionid" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span v-if="scope.row.unionid">{{scope.row.unionid}}</span>
          <span v-else>暂无</span>
        </template>
      </el-table-column>
      <el-table-column label="openid" align="center" prop="openid" :show-overflow-tooltip="true"/>
      <el-table-column label="用户类型" align="center" prop="utype">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.wx_usr_utype" :value="scope.row.utype"/>
        </template>
      </el-table-column>
      <el-table-column label="手机号" align="center" prop="phone" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <span v-if="scope.row.phone">{{scope.row.phone}}</span>
          <span v-else>暂无</span>
        </template>
      </el-table-column>
      <el-table-column label="用户昵称" align="center" prop="nickname">
        <template slot-scope="scope" :show-overflow-tooltip="true">
          <span v-if="scope.row.nickname">{{scope.row.nickname}}</span>
          <span v-else>暂无</span>
        </template>
      </el-table-column>
      <el-table-column label="用户姓名" align="center" prop="realName">
        <template slot-scope="scope" :show-overflow-tooltip="true">
          <span v-if="scope.row.realName">{{scope.row.realName}}</span>
          <span v-else>暂无</span>
        </template>
      </el-table-column>
      <el-table-column label="用户头像" align="center" prop="avatar" width="100">
        <template slot-scope="scope">
          <image-preview :src="scope.row.avatar" :width="50" :height="50"/>
        </template>
      </el-table-column>
      <el-table-column label="用户性别" align="center" prop="sex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex"/>
        </template>
      </el-table-column>
      <el-table-column label="最近登陆" align="center" prop="lastLoginTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastLoginTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="绑定状态" align="center" prop="hasDoctor">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.hasDoctor === 1">已绑定</el-tag>
          <el-tag type="info" v-else>未绑定</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="用户状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.wx_user_type" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['wexin:user:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['wexin:user:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改微信小程序用户对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="40%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="unionid" prop="unionid">
          <el-input :disabled="true" v-model="form.unionid" placeholder="请输入unionid" />
        </el-form-item>
        <el-form-item label="openid" prop="openid">
          <el-input :disabled="true" v-model="form.openid" placeholder="请输入openid" />
        </el-form-item>
        <el-form-item label="用户类型" prop="utype">
          <el-select v-model="form.utype" placeholder="请选择用户类型">
            <el-option
              v-for="dict in dict.type.wx_usr_utype"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户手机" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入用户手机" />
        </el-form-item>
        <el-form-item label="用户性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择用户性别">
            <el-option
              v-for="dict in dict.type.sys_user_sex"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户昵称">
          <el-input v-model="form.nickname" placeholder="请输入用户昵称" />
        </el-form-item>
        <el-form-item label="用户头像">
          <image-upload :limit="1" v-model="form.avatar"/>
        </el-form-item>
        <el-form-item label="用户城市" prop="city">
          <el-input v-model="form.city" placeholder="请输入用户城市" />
        </el-form-item>
        <el-form-item label="用户地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入用户地址" />
        </el-form-item>
        <el-form-item label="用户状态" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.wx_user_type"
              :key="dict.value"
              :label="parseInt(dict.value)"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="用户备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入用户备注" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button :loading="buttonLoading" type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUser, getUser, delUser, addUser, updateUser } from "@/api/weixin/user";

export default {
  name: "WxUser",
  dicts: ['wx_usr_utype', 'wx_user_type', 'sys_user_sex'],
  data() {
    return {
      // 按钮loading
      buttonLoading: false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 微信小程序用户表格数据
      userList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        utype: undefined,
        phone: undefined,
        nickname: undefined,
        sex: undefined,
        city: undefined,
        address: undefined,
        avatar: undefined,
        identity: undefined,
        status: undefined,
        remark: undefined
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        utype: [
          { required: true, message: "用户类型不能为空", trigger: "change" }
        ],
        phone: [
          { required: true, message: "手机不能为空", trigger: "blur" }
        ],
        sex: [
          { required: true, message: "用户性别不能为空", trigger: "change" }
        ],
        status: [
          { required: true, message: "状态不能为空", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询患者信息 */
    patientInfoHandle(){
      
    },
    /** 查询微信小程序用户列表 */
    getList() {
      this.loading = true;
      listUser(this.queryParams).then(response => {
        this.userList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        unionid: undefined,
        openid: undefined,
        utype: undefined,
        phone: undefined,
        nickname: undefined,
        sex: undefined,
        avatar: undefined,
        lastLoginTime: undefined,
        status: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.loading = true;
      this.reset();
      const id = row.id || this.ids
      getUser(id).then(response => {
        this.loading = false;
        this.form = response.data;
        this.form.avatar = response.data.ossId;
        console.log(this.form);
        this.open = true;
        this.title = "修改微信小程序用户";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.form.lastLoginTime = null;
      this.form.unionid = null;
      this.form.openid = null;
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.buttonLoading = true;
          if (this.form.id != null) {
            updateUser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          } else {
            addUser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            }).finally(() => {
              this.buttonLoading = false;
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除微信小程序用户编号为"' + ids + '"的数据项？').then(() => {
        this.loading = true;
        return delUser(ids);
      }).then(() => {
        this.loading = false;
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      }).finally(() => {
        this.loading = false;
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('wx/user/export', {
        ...this.queryParams
      }, `user_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
